//
//  CERuleVC.m
//  digitalCurrency
//
//  Created by 来迎宾 on 2019/4/4.
//  Copyright © 2019年 XinHuoKeJi. All rights reserved.
//

#import "CERuleVC.h"
#import "CERuleView.h"
#import "CERuleListCell.h"
#import "HomeNetManager.h"
#import "CERuleLevelModel.h"

@interface CERuleVC ()
@property (nonatomic, strong) UIView *sectionView;
@property (nonatomic, strong) NSMutableArray *ruleList;
@property (nonatomic, strong) CERuleView *headerview;
@property (nonatomic, strong) UIView *headShrinkView;
@end

@implementation CERuleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.viewTitle.text = LocalizationKey(@"Reward rules");
    self.ruleList = [NSMutableArray array];
    
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NAVIGATION_HEIGHT_S);
    [self.view addSubview:self.tableView];
    self.tableView.separatorColor   = [UIColor clearColor];
    self.tableView.rowHeight = 49;

    [self setTablewViewHeard];
    [self.tableView registerClass:[CERuleListCell class] forCellReuseIdentifier:@"ruleCell"];
    [self headRefreshWithScrollerView:self.tableView];
    
    [self getBonusesLevel];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

#pragma mark- CE/invest/getBonusesLevel
- (void)getBonusesLevel {
    [HomeNetManager getBonusesLevelWithCompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            NSArray *data = resPonseObj[@"data"];
            if (codeInt == 0) {
                [self.ruleList removeAllObjects];
                for (NSDictionary *dic  in data) {
                    CERuleLevelModel *model = [CERuleLevelModel mj_objectWithKeyValues:dic];
                    [self.ruleList addObject:model];
                }
                
                [self.tableView reloadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

//MARK:--下拉刷新
- (void)refreshHeaderAction {
    [self getBonusesLevel];
}

- (void)click {
    
}

-(void)setTablewViewHeard {
    //展view
    self.headerview = [[CERuleView alloc]initWithFrame:CGRectMake(0, 0, kWindowW,  405)];
    self.headerview.backgroundColor = [UIColor colorWithHEX:mainBlackColor];
    self.tableView.tableHeaderView  = self.headerview;
    
    SYButton *Button = [SYButton buttonWithType:UIButtonTypeCustom];
    [self.headerview addSubview:Button];
    Button.btnType = RIGHT;
    [Button setTitle:LocalizationKey(@"Pack up") forState:UIControlStateNormal];
    [Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    Button.titleLabel.font = kFont(14);
    [Button setImage:[UIImage imageNamed:@"arrows_shang"] forState:UIControlStateNormal];
    [Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(-16);
        make.centerX.equalTo(0);
    }];
    [Button addTarget:self action:@selector(buttonAct) forControlEvents:UIControlEventTouchUpInside];
    
    
    //缩view
    self.headShrinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 70)];
    self.headShrinkView.backgroundColor = [UIColor colorWithHEX:mainBlackColor];

    UIView *shrinkSubView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 55)];
    [self.headShrinkView addSubview:shrinkSubView];
    shrinkSubView.backgroundColor = [UIColor colorWithHEX:@"C0C2C6"];
    shrinkSubView.layer.masksToBounds = YES;
    shrinkSubView.layer.cornerRadius = 5;
    
    UILabel *shrinkSubLB = [[UILabel alloc]init];
    [shrinkSubView addSubview:shrinkSubLB];
    [shrinkSubLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.centerY.equalTo(0);
    }];
    shrinkSubLB.text = LocalizationKey(@"View Detailed Rules");
    shrinkSubLB.font = kFont(15);
    shrinkSubLB.textColor = [UIColor colorWithHEX:mainBlackColor];
    
    SYButton *shrinkSubBtn = [SYButton buttonWithType:UIButtonTypeCustom];
    [shrinkSubView addSubview:shrinkSubBtn];
    shrinkSubBtn.btnType = RIGHT;
    [shrinkSubBtn setTitle:LocalizationKey(@"Open") forState:UIControlStateNormal];
    [shrinkSubBtn setTitleColor:[UIColor colorWithHEX:mainBlackColor] forState:UIControlStateNormal];
    shrinkSubBtn.titleLabel.font = kFont(15);
    [shrinkSubBtn setImage:[UIImage imageNamed:@"arrows_black_xia"] forState:UIControlStateNormal];
    [shrinkSubBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-20);
        make.centerY.equalTo(0);
    }];
    [shrinkSubBtn addTarget:self action:@selector(shrinkSubBtnAct) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)buttonAct{
    self.headerview.hidden = YES;
    self.headShrinkView.hidden = NO;
    self.tableView.tableHeaderView  = self.headShrinkView;
}

- (void)shrinkSubBtnAct{
    self.headerview.hidden = NO;
    self.headShrinkView.hidden = YES;
    self.tableView.tableHeaderView  = self.headerview;
}

    
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 64;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.ruleList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CERuleListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ruleCell"];
   
    CERuleLevelModel *model = self.ruleList[indexPath.row];
    cell.levelLabel.text = [NSString stringWithFormat:@"%@",[self level:model.level]];
    cell.midLabel.text   = [NSString stringWithFormat:@"%@-%@",model.nimIndex,model.maxInde];
    cell.lastLabel.text  = [NSString stringWithFormat:@"%@",[self rate:model.bonusesRatio]];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (NSString *)level:(NSNumber *)level {
    switch ([level integerValue]) {
        case 1:
            return LocalizationKey(@"first bonus");
            break;
        case 2:
            return LocalizationKey(@"second bonus");
            break;
        case 3:
            return LocalizationKey(@"third bonus");
            break;
        case 4:
            return LocalizationKey(@"forth bonus");
            break;
        case 5:
            return LocalizationKey(@"fifth bonus");
            break;
        case 6:
            return LocalizationKey(@"sixth bonus");
            break;
        case 7:
            return LocalizationKey(@"seventh bonus");
            break;
        case 8:
            return LocalizationKey(@"eighth bonus");
            break;
        case 9:
            return LocalizationKey(@"ninth bonus");
            break;
        case 10:
            return LocalizationKey(@"ten bonus");
            break;
        default:
            break;
    }
    
    return LocalizationKey(@"");
}

- (NSString *)rate:(NSString *)rate {
    CGFloat nrate = [rate floatValue] * 100;
    return [NSString stringWithFormat:@"%.2f%%",nrate];
}

#pragma mark-自定义section头部的高度
- (UIView *)sectionView {
    if (!_sectionView) {
        CGFloat viewwidth  = SCREEN_WIDTH;
        CGFloat labelwidth = (viewwidth-30)/3.0;
        _sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewwidth, 64)];
        _sectionView.backgroundColor = [UIColor colorWithHEX:mainBlackColor];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewwidth, 15)];
        lineView.backgroundColor = [UIColor blackColor];
        [_sectionView addSubview:lineView];
        
        UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 30, labelwidth, 19)];
        l1.text = LocalizationKey(@"Reward level");
        l1.textColor = [UIColor whiteColor];
        l1.font = [UIFont systemFontOfSize:15];
        [_sectionView addSubview:l1];
        
        UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(15+labelwidth, 30, labelwidth, 19)];
        l2.text = LocalizationKey(@"Ranking");
        l2.textColor = [UIColor whiteColor];
        l2.font = [UIFont systemFontOfSize:15];
        l2.textAlignment = NSTextAlignmentCenter;
        [_sectionView addSubview:l2];
        
        UILabel *l3 = [[UILabel alloc] initWithFrame:CGRectMake(15+labelwidth*2, 30, labelwidth, 19)];
        l3.text = LocalizationKey(@"Reward proportion");
        l3.textColor = [UIColor whiteColor];
        l3.font = [UIFont systemFontOfSize:15];
        l3.textAlignment = NSTextAlignmentRight;
        [_sectionView addSubview:l3];
    }
    
    return _sectionView;
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}

@end
